//
//  TopApi.m
//  mcf
//
//  Created by yuike6 yuike6 on 12-1-9.
//  Copyright (c) 2012年 yuike. All rights reserved.
//

#import "TopApi.h"
#import "TopApiDef.h"
#import "header.h"

@implementation TopApi

@synthesize appKey;
@synthesize appSecret;

@synthesize sinceTime;
- (void) setSinceTime:(NSTimeInterval)sinceTimeValue
{
    if (sinceTime <= 0) {
        sinceTime = sinceTimeValue;
    }
}

@synthesize apiContent = api_Content_;
-(NSString *) apiContent
{
    if(api_Content_ == nil ||
       [api_Content_ length] <= 0)
    {
        api_Content_ = [[NSString alloc] initWithString:TOP_API_TEMPLATE_WIRELESS];
    }
    
    return api_Content_;
}

@synthesize time = time_;
- (NSString *) time
{
    YRelease(time_);
    time_ = [[NSString alloc] initWithString:[NSDate date2string:[NSDate date]]];
    return time_;
}

-(void) dealloc
{
    YRelease(api_Content_)
    YRelease(time_)
    
    
    YRelease(appKey);
    
    YRelease(appSecret);
    
    
    [super dealloc];
}

-(id) init
{
    self = [super init];
    if(self)
    {
        self.sinceTime = 0;
    }
    
    return self;
}

-(void) relpaceTopApiValueByKey:(NSString *) apiValue_ key:(NSString *) keyValue
{
    
    NSString * value = [self.apiContent replace:keyValue with:apiValue_];
    
    self.apiContent = value;
}

-(void) setTopApi:(NSString *) topApiValue
{    
    [self relpaceTopApiValueByKey:topApiValue key:_PARAM_TOP_API_];
}

-(void) setTopApiV:(NSString *) vValue
{
    if(vValue == nil ||
       [vValue length] <= 0)
    {
        [self relpaceTopApiValueByKey:@"*" key:_PARAM_TOP_V_];
    }
    else
    {
        [self relpaceTopApiValueByKey:vValue key:_PARAM_TOP_V_];
    }
}

-(void) setTopApiIMEI:(NSString *) imeiValue
{    
    [self relpaceTopApiValueByKey:imeiValue key:_PARAM_TOP_IMEI_];
}

-(void) setTopApiIMSI:(NSString *) imsiValue
{    
    [self relpaceTopApiValueByKey:imsiValue key:_PARAM_TOP_IMSI_];
}

-(void) setTopApiAppkey:(NSString *) appKeyValue
{    
    [self relpaceTopApiValueByKey:appKeyValue key:_PARAM_TOP_APPKEY_];
}

-(void) setTopApiSign:(NSString *) singValue
{    
    /*
     参数签名,分两种:
     1 未登录公式: md5(appSecret+api+v+imei+imsi+md5(data)+t)如果涉及到编码请使用gbk编码进行加 签
     2 已登录公式:
     md5 (ecode+appSecret+api+v+imei+imsi+m d5(data)+t) 其中的ecode是登录后登录接口 返回的
     
     */
    [self relpaceTopApiValueByKey:singValue key:_PARAM_TOP_SIGN_];
}

-(void) setTopApiT:(NSString *) tValue
{    
    if(tValue == nil ||
       [tValue length] <= 0)
    {
        NSDate * date_ = [NSDate date];
        NSString * tValue_ = [NSString stringWithFormat:@"%f",[date_ timeIntervalSince1970]];
        [self relpaceTopApiValueByKey:tValue_ key:_PARAM_TOP_T_];
    }
    else
    {
        [self relpaceTopApiValueByKey:tValue key:_PARAM_TOP_T_];
    }
    
}

-(void) setTopApiTTID:(NSString *) ttidValue
{    
    [self relpaceTopApiValueByKey:ttidValue key:_PARAM_TOP_TTID_];
}

-(void) setTopApiData:(NSString *) dataValue
{    
    [self relpaceTopApiValueByKey:dataValue key:_PARAM_TOP_DATA_];
}

-(NSString *) topApiUrl
{
    return self.apiContent;
}

-(NSString *) topApiValided
{
    NSArray * theArray = [[NSArray alloc] initWithObjects:_PARAM_TOP_API_,
                        _PARAM_TOP_V_,
                        _PARAM_TOP_IMEI_,
                        _PARAM_TOP_IMSI_,
                        _PARAM_TOP_APPKEY_,
                        _PARAM_TOP_DATA_,
                        _PARAM_TOP_SIGN_,
                        _PARAM_TOP_T_,
                        _PARAM_TOP_TTID_,
                        nil];
    
    [theArray autorelease];
    
    for (NSString * key in theArray) {
        if([self.apiContent isIncludeSubString:key])
        {
            return key;
        }
    }
    
    return nil;
}

-(BOOL) istopApiValided
{
    return [self topApiValided]== nil ? TRUE : FALSE;
}

@end
